package com.bxd.baodan.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.bxd.baodan.R;
import com.bxd.baodan.adapter.CommonAdapter;
import com.bxd.baodan.adapter.ViewHolder;
import com.bxd.baodan.item.FamDesignBean.ValueBean;
import com.bxd.baodan.util.LogUtil;
import com.bxd.baodan.util.StringUtil;
import com.bxd.baodan.widget.MyGridView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/**
 *
 */

public class MemberFragment extends BaseStepFragment {

    private MyGridView gv;
    private View topView;

    private List<ValueBean> memberList = new ArrayList<>();
    // private List<String> checkIds = new ArrayList<>() ;

    CommonAdapter<ValueBean> commonAdapter;
    private HashSet<String> set = new HashSet<>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        LogUtil.e("MemberFragment: -------CreateView");
        return inflater.inflate(R.layout.step_5_family, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        title.setText(roleText + "的家庭成员");

        gv = (MyGridView) view.findViewById(R.id.gv);
        topView = view.findViewById(R.id.title);

        // FIXME: 17/7/14 解决listView跳动问题,将焦点设置到顶部view就行了
        topView.setFocusable(true);
        topView.setFocusableInTouchMode(true);
        topView.requestFocus();
        memberList.clear();

        memberList.addAll(getBean().data.condition.family_member.value);

        commonAdapter = new CommonAdapter<ValueBean>(getActivity(), memberList, R.layout.step_3_item) {
            @Override
            public void convert(ViewHolder holder, final ValueBean item) {

                if (!StringUtil.isEmpty(getDesignStep().family_member)) {
                    String a = getDesignStep().family_member;
                    String[] b = a.split(",");
                    for (String aB : b) {
                        if (item.id.equals(aB)) {
                            ((CheckBox) holder.getView(R.id.check)).setChecked(true);
                        }

                    }

                }
                ((CheckBox) holder.getView(R.id.check)).setText(item.name);
                if (item.id.equals("my")) {
                    holder.getView(R.id.check).setVisibility(View.GONE);
                }
                ((CheckBox) holder.getView(R.id.check)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {


                            set.add(item.id);

                        } else {
                            try {
                                set.remove(item.id);
                            } catch (Exception e) {

                            }

                        }
                        StringBuilder builder = new StringBuilder();

                        // FIXME: 17/9/16 这里用list有一个奇葩的问题,就是选择输入框时会add一个配偶,以后研究
//
//                        for (int i = 0; i < set.size(); i++) {
//                            if (i == checkIds.size() - 1) {
//                                builder.append(checkIds.get(i));
//                            } else {
//                                builder.append(checkIds.get(i)+",");
//                            }
//                        }

                        Iterator<String> iterator = set.iterator();
                        while (iterator.hasNext()) {
                            builder.append(iterator.next());
                            if (iterator.hasNext()) {
                                builder.append(",");
                            }
                        }

                        getDesignStep().family_member = builder.toString();


                    }
                });

            }
        };

        gv.setAdapter(commonAdapter);


    }


    @Override
    public void onDestroyView() {
        LogUtil.e("MemberFragment------: onDestroyView");
        super.onDestroyView();
    }

}
